package com.motorola.stylus.view.recyclerview

import android.graphics.Canvas
import android.view.View
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.ItemTouchUIUtil
import androidx.recyclerview.widget.RecyclerView

/**
 * Original see [androidx.recyclerview.widget.ItemTouchUIUtilImpl].
 * Used in [ItemTouchHelperPlus]
 */
class ItemTouchUIUtilImplKt : ItemTouchUIUtil {
    override fun onDraw(
        c: Canvas, recyclerView: RecyclerView, view: View, dX: Float, dY: Float,
        actionState: Int, isCurrentlyActive: Boolean
    ) {
        view.translationX = dX
        view.translationY = dY
    }

    override fun onDrawOver(
        c: Canvas, recyclerView: RecyclerView, view: View, dX: Float, dY: Float,
        actionState: Int, isCurrentlyActive: Boolean
    ) {
    }

    override fun clearView(view: View) {
        view.translationX = 0f
        view.translationY = 0f
    }

    override fun onSelected(view: View) {}

    companion object {
        val INSTANCE: ItemTouchUIUtil = ItemTouchUIUtilImplKt()
        fun findMaxElevation(recyclerView: RecyclerView, itemView: View): Float {
            val childCount = recyclerView.childCount
            var max = 0f
            for (i in 0 until childCount) {
                val child = recyclerView.getChildAt(i)
                if (child === itemView) {
                    continue
                }
                val elevation = ViewCompat.getElevation(child)
                if (elevation > max) {
                    max = elevation
                }
            }
            return max
        }
    }
}